package com.sjq.supplier.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjq.supplier.entity.Admininfo;
import com.sjq.supplier.entity.Supplier;
import com.sjq.supplier.service.AdmininfoService;
import com.sjq.supplier.service.SupplierService;
import com.sjq.supplier.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (user)控制层
 *
 * @author makejava
 * @since 2024-01-02 14:54:49
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private AdmininfoService admininfoService;
    /**
     * 服务对象
     */
    @Resource
    private SupplierService supplierService;


    //登录接口
    @PostMapping("login")
    public Result<?> login(String account,String password,Integer role){
        //判断参数是否为空
        if (account==null||password==null||role==null){
            return Result.failure("账号、密码和身份不能为空");
        }
        //account转换大写
        account = account.toUpperCase();
        if (role==1){
            QueryWrapper<Admininfo> admininfoQueryWrapper = new QueryWrapper<>();
            admininfoQueryWrapper.eq("account",account);
            admininfoQueryWrapper.last("LIMIT 1");
            Admininfo one = admininfoService.getOne(admininfoQueryWrapper);
            if (one!=null){
                if (one.getPassword().equals(password)){
                    return Result.success("登录成功",one);
                }else {
                    return Result.failure("密码错误");
                }
            }else {
                return Result.failure("账号不存在");
            }
        }else if (role==2){
            QueryWrapper<Supplier> supplierQueryWrapper = new QueryWrapper<>();
            supplierQueryWrapper.eq("account",account);
            supplierQueryWrapper.last("LIMIT 1");
            Supplier one = supplierService.getOne(supplierQueryWrapper);
            if (one!=null){
                if (one.getPassword().equals(password)){
                    return Result.success("登录成功",one);
                }else {
                    return Result.failure("密码错误");
                }
            }else {
                return Result.failure("账号不存在");
            }
        }
        return Result.failure("身份不存在");
    }

    //注册接口
    @PostMapping("register")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> register(String account,String password,String username,Integer role){
        //account转换大写
        account = account.toUpperCase();
        //判断账号是否重复
        Long count = supplierService.lambdaQuery().eq(Supplier::getAccount, account).count();
        if(count>0){
            return Result.failure("账号已存在，请更换账号或者直接登录。");
        }
        Supplier supplier = new Supplier();
        supplier.setAccount(account);
        supplier.setPassword(password);
        supplier.setUsername(username);
        supplier.setRole(role);
        supplier.setIsvalid(1);

        if (supplierService.save(supplier)) {
            return Result.success("注册成功");
        } else {
            return Result.failure("注册失败");
        }

    }
}

